package com.dq.domain.message.oneself;

import com.alibaba.fastjson.JSONObject;
import com.dq.domain.subscribe.Subscribe;
import com.dq.vo.baseVo.VO;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @author liuhui
 * @date 2019/11/22 0022 10:30:09
 */
@Data
@Entity
@ApiModel("预约消息")
public class SubscribeMessage extends Message {

    @ApiModelProperty("关联预约实体对象")
    @ManyToOne
    public Subscribe subscribe;
    @ApiModelProperty("消息提示内容")
    @Column(columnDefinition = "text")
    public String content;

    @ApiModelProperty("消息接受者，员工/顾客")
    @ManyToOne
    public SysUser caseUser;

    @ApiModelProperty("接收者类型：(员工/顾客)")
    @Enumerated(EnumType.ORDINAL)
    @NotNull
    protected UserType userType;

    public enum UserType{
        /**
         * 接收者类型：(员工/顾客)
         */
        staff("员工"),
        client("顾客");

        public String name;

        UserType(String name){
            this.name = name;
        }
    }


    @Data
    @ApiModel("视图")
    public static class Vo implements VO {
        protected Long id;

        @ApiModelProperty("未读/已读")
        public String messageStatusEn;
        @ApiModelProperty("未读/已读")
        public String messageStatusCn;
        @ApiModelProperty("消息类型")
        public String messageTypeEn;
        @ApiModelProperty("消息类型")
        public String messageTypeCn;
        @ApiModelProperty("主题")
        public String theme;
        @ApiModelProperty("关联预约单")
        public JSONObject subscribe;
        @ApiModelProperty("创建时间")
        public String createTime;
    }

    @Override
    public VO toVO(boolean isRecursion) {
        Vo vo = new Vo();
        vo.id = this.id;

        vo.messageStatusEn = this.getMessageStatus().toString();
        vo.messageStatusCn = this.getMessageStatus().name;
        vo.messageTypeEn = this.getMessageType().toString();
        vo.messageTypeCn = this.getMessageType().name;
        vo.theme = fmt(this.getSubscribe().getWxUser().getUserRealcName());
        vo.createTime = fmt(this.createTime, TimeUtil.YYYY_MM_DD_HH_MM_SS);

        // 预约单信息
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", this.subscribe.getId());
        jsonObject.put("orgName", this.subscribe.getSysDept() == null ? null : this.subscribe.getSysDept().getSimpleName());
        jsonObject.put("staffUserName", this.subscribe.getTechnicianUser() == null ? null : this.subscribe.getTechnicianUser().getUserRealcName());
        jsonObject.put("beginTime",fmt(this.subscribe.getBeginTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS));
        jsonObject.put("endTime",fmt(this.subscribe.getEndTime(), TimeUtil.YYYY_MM_DD_HH_MM_SS));
        jsonObject.put("subscribeStatusEn", this.subscribe.getSubscribeStatus().toString());
        jsonObject.put("subscribeStatusCn", this.subscribe.getSubscribeStatus().name);
        jsonObject.put("theme", fmt(this.getSubscribe().getWxUser().getUserRealcName()));
        jsonObject.put("feedBack",fmt(this.getSubscribe().getFeedBack()));
        vo.subscribe = jsonObject;

        return vo;
    }
}
